package com.tengju.user.infrastructure.repository.archive;

import com.tengju.user.domain.model.archive.letax.*;
import com.tengju.user.domain.model.user.UserIdCode;
import com.tengju.user.domain.shared.AccountId;
import com.tengju.user.infrastructure.persistence.mybatis.dataobject.LetaxWithdrawDOWithBLOBs;
import com.tengju.user.infrastructure.persistence.mybatis.mapper.dealer.generate.LetaxWithdrawDOMapper;
import java.util.stream.Collectors;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class LetaxWithdrawRepositoryImpl implements LetaxWithdrawRepository {

    private final LetaxWithdrawDOMapper letaxWithdrawDOMapper;

    public LetaxWithdrawRepositoryImpl(LetaxWithdrawDOMapper letaxWithdrawDOMapper) {
        this.letaxWithdrawDOMapper = letaxWithdrawDOMapper;
    }

    @Override
    public int count(UserIdCode idCode, AccountId accountId, List<LetaxWithdrawStatus> statusList,
            IdentityType identityType) {
        return letaxWithdrawDOMapper.count(idCode.getValue(), accountId.getValue(),
                identityType.getVal(),
                statusList.stream().map(LetaxWithdrawStatus::getVal).collect(Collectors.toList()));
    }

    @Override
    public LetaxWithdraw find(LetaxWithdrawId letaxWithdrawId) {
        LetaxWithdrawDOWithBLOBs letaxWithdrawDOWithBLOBs = letaxWithdrawDOMapper.selectByPrimaryKey(letaxWithdrawId.getValue());
        if(letaxWithdrawDOWithBLOBs == null){
            return null;
        }
        return new LetaxWithdraw(new LetaxWithdrawId(letaxWithdrawDOWithBLOBs.getId())
                ,LetaxWithdrawStatus.formVal(letaxWithdrawDOWithBLOBs.getStatus())
                ,IdentityType.fromVal(letaxWithdrawDOWithBLOBs.getAuthType()));
    }
}
